//
//  UIViewController+LoadingPresentingBacking.m
//  Elephant
//
//  Created by enghou on 2018/5/21.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "UIViewController+LoadingPresentingBacking.h"
#import "NetPath.h"
@implementation UIViewController (LoadingPresentingBacking)

-(void)commonNetFailBlockWithBlock:(void(^)(void))block{
//    [self endLoading];
//    NetWorkFailViewController *fail = [[NetWorkFailViewController alloc]init];
//    fail.refresh = block;
//    [self presentVC:fail];
}

-(UIView *)inputAccessView{
    UIView *tray = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    tray.backgroundColor = [UIColor whiteColor];
    tray.layer.borderColor = [UIColor lightGrayColor].CGColor;
    tray.layer.borderWidth = 0.5;
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
    cancel.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancel setFrame:CGRectMake(0, 0, 80, 40)];
    
    [cancel addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [tray addSubview:cancel];
    
    UIButton *sure = [UIButton buttonWithType:UIButtonTypeSystem];
    [sure setTitle:@"确定" forState:UIControlStateNormal];
    [sure setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
    sure.titleLabel.font = [UIFont systemFontOfSize:16];
    [sure setFrame:CGRectMake(ScreenWidth - 80, 0, 80, 40)];
    [sure addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [tray addSubview:sure];
    UIView *separator = [[UIView alloc]init];
    separator.backgroundColor = [UIColor lightGrayColor];
    [separator setFrame:CGRectMake(0, tray.frame.origin.y + tray.frame.size.height - 0.5, ScreenWidth, 0.5)];
    [tray addSubview:separator];
    return tray;
}

-(void)cancelAction{
    
}

-(void)sureAction{
    
}

-(void)commonPopBlockWithModel:(BaseModel *)model{
    [self endLoading];
    if (model.success) {
        [self pop];
    }else{
        [self alertSome:model.msg success:model.success];
    }
}

-(void)beginLoading{
    [SVProgressHUD show];
}

-(void)endLoading{
    [SVProgressHUD dismiss];
}

-(void)dismissBackButton{
    self.navigationItem.leftBarButtonItem = nil;
}

-(void)alertView:(UIView *)view{
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    self.definesPresentationContext = YES;
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self.view addSubview:view];
}

-(void)alertSome:(NSString *)msg success:(BOOL)success{
//    [self endLoading];
//    CommonAlertViewController *alert = [[CommonAlertViewController alloc]init];
//    alert.success = success;
//    alert.tip = msg;
//    if (!success) {
//        [self presentVC:alert];
//    }
}

-(void)alertSome:(NSString *)msg{
    [SVProgressHUD showInfoWithStatus:msg];
    [SVProgressHUD dismissWithDelay:1];
}

-(void)alertSome:(NSString *)msg sureAction:(void(^)(void))act{
//    [self alertSome:msg sureAction:act cancelAct:nil];
}

-(void)alertSome:(NSString *)msg sureAction:(void (^)(void))act cancelAct:(void(^)(void))cancel{
//    [self alertSome:msg sureTitle:@"确定" sureAction:act cancelTitle:@"取消" cancelAct:cancel];
}

-(void)presentVC:(UIViewController *)vc{
    [self presentVC:vc completeBlock:nil];
}

-(void)presentVC:(UIViewController *)vc completeBlock:(void(^)(void))complete{
    self.definesPresentationContext = YES;
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    vc.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    vc.transitioningDelegate = self;
    [self.navigationController presentViewController:vc animated:NO completion:complete];
}

-(UIButton *)defaultBackButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    UIImage *image = [UIImage imageNamed:@"nav_return"];
    [button setImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 50, 44);
    [button addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = item;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    return button;
}

-(void)alertSome:(NSString *)msg sureTitle:(NSString *)sureTitle sureAction:(void(^)(void))sureAct cancelTitle:(NSString *)cancelTitle cancelAct:(void(^)(void))cancel{
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        if (sureAct) {
//            sureAct();
//        }
//    }];
//    [alert addAction:sureAction];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        if (cancel) {
//            cancel();
//        }
//    }];
//    [alert addAction:cancelAction];
//
//    [self presentViewController:alert animated:YES completion:nil];
}

-(void)push:(UIViewController *)vc{
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma Mark - Animation Delegate
//- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
//    PresentAnimator *animation = [[PresentAnimator alloc]init];
//    animation.duration = 0.25;
//    animation.presenting = YES;
//    return animation;
//}

//- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
//    return nil;
//}

//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator{
//    return nil;
//}
//
//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator{
//    return nil;
//}
//
//- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source{
//    return nil;
//}

@end
